﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Collections;
using PsychveyApp1.classes;

namespace PsychveyApp1.classes
{
    public class MultipleSelectionAnswerSurveyPanel : System.Web.UI.WebControls.Panel
    {
        public CheckBox cbxAnswerCheckbox;
        public Table tableMain;
        public Survey currentSurvey;
        public UserAccount currentUser;
        public Question currentQuestion;
        public Button btnSubmit;
        public Table secondTable;
        public ArrayList answerLabelArray;
        public ArrayList answerCheckboxArray;

        public Label label;
        public Label questionText;
        public Label questionStatus;
        public ArrayList listOfAnswers;
        public ArrayList listOfQuestion;
        public ArrayList listOfUserAnswers;

        

        public MultipleSelectionAnswerSurveyPanel(UserAccount user, Survey survey, Question question, ArrayList listOfAnswers, ArrayList listOfUserAnswers)
            : base()
        {
            currentUser = user;
            currentSurvey = survey;
            currentQuestion = question;
            this.listOfAnswers = listOfAnswers;
            this.listOfUserAnswers = listOfUserAnswers;

            tableMain = new Table();
            TableRow row = new TableRow();
            TableCell cell = new TableCell();

            label = new Label();
            cell.ColumnSpan = 2;
            label.Text = "Question No: " + question.questionOrder;
            cell.Controls.Add(label);
            tableMain.Rows.Add(row);
            row.Cells.Add(cell);


            //--------------------------
            row = new TableRow();
            cell = new TableCell();
            Label questionText = new Label();
            questionText.Text = question.questionsAsked.ToString();
            cell.ColumnSpan = 2;
            cell.Controls.Add(questionText);
            row.Cells.Add(cell);
            tableMain.Rows.Add(row);


            row = new TableRow();
            cell = new TableCell();
            questionStatus = new Label();
            cell.ColumnSpan = 2;
            cell.Controls.Add(questionStatus);
            row.Cells.Add(cell);
            tableMain.Rows.Add(row);

            //--------------------------
            this.answerLabelArray = new ArrayList();
            this.answerCheckboxArray = new ArrayList();
            for (int i = 0; i < listOfAnswers.Count; i++)
            {
                row = new TableRow();
                cell = new TableCell();

                //temporary variable, cannot reuse
                cbxAnswerCheckbox = new CheckBox();
                this.answerCheckboxArray.Add(cbxAnswerCheckbox);
                cbxAnswerCheckbox.Text = ((Answer)listOfAnswers[i]).answerText;
                if (answerExists(((Answer)listOfAnswers[i]).answerID) == true)
                {
                    cbxAnswerCheckbox.Checked = true;
                }
                cell.Controls.Add(cbxAnswerCheckbox);
                row.Cells.Add(cell);
                tableMain.Rows.Add(row);

                //lblAnswer = new Label();
                //lblAnswer.Text = ((Answer)listOfAnswers[i]).answerText;
                //this.answerLabelArray.Add(lblAnswer);
                //cell.Controls.Add(lblAnswer);
                //row.Cells.Add(cell);
                //tableMain.Rows.Add(row);


            }

            row = new TableRow();
            cell = new TableCell();
            cell.ColumnSpan = 2;
            Image breakLine = new Image();
            breakLine.ImageUrl = ("~/images/breakline.png");
            cell.Controls.Add(breakLine);
            row.Controls.Add(cell);
            tableMain.Rows.Add(row);

            this.Controls.Add(tableMain);

        }

        //check whether an answer exists in the user answer table (of the current user) or not
        public bool answerExists(string answerID)
        {
            bool outcome = false;
            for (int i = 0; i < this.listOfUserAnswers.Count; i++)
            {
                if (((UserAnswer)listOfUserAnswers[i]).answerID.Equals(answerID))
                {
                    outcome = true;
                }

            }


            return outcome;
        }
    }
    }
